import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
%matplotlib inline
from sklearn.datasets import make_circles
# generate 2d classification dataset
X, y = make_circles(n_samples=100, noise=0.1)
# scatter plot, dots colored by class value
df = pd.DataFrame(dict(X1=X[:,0], X2=X[:,1], Y=y))
colors = {0:'red', 1:'blue'}
fig, ax = plt.subplots()
grouped = df.groupby('Y')
for key, group in grouped:
group.plot(ax=ax, kind='scatter', x='X1', y='X2', label=key, color=colors[key])
plt.show()
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.3)
from sklearn.svm import SVC
linear=SVC(kernel='linear')
linear.fit(X_train,y_train)
SVC(kernel='linear')
from sklearn.metrics import accuracy_score
pred=linear.predict(X_test)
accuracy_score(y_test,pred)
0.4666666666666667
df
| X1 | X2 | Y | |
|---|---|---|---|
| 0 | -0.755754 | -0.701927 | 0 |
| 1 | -0.324131 | 0.631230 | 1 |
| 2 | 0.512493 | 1.065001 | 0 |
| 3 | 0.156620 | -0.766278 | 1 |
| 4 | 0.479098 | 0.667054 | 1 |
| ... | ... | ... | ... |
| 95 | 0.049305 | 1.025803 | 0 |
| 96 | -1.148978 | 0.323755 | 0 |
| 97 | -0.626174 | 0.605153 | 0 |
| 98 | 0.961855 | 0.403435 | 0 |
| 99 | -0.379807 | -0.473364 | 1 |
100 rows × 3 columns
df['X3']=df['X1']**2
df['X4']=df['X2']**2
df['X5']=df['X1']*df['X2']
df
| X1 | X2 | Y | X3 | X4 | X5 | |
|---|---|---|---|---|---|---|
| 0 | -0.755754 | -0.701927 | 0 | 0.571164 | 0.492702 | 0.530485 |
| 1 | -0.324131 | 0.631230 | 1 | 0.105061 | 0.398452 | -0.204601 |
| 2 | 0.512493 | 1.065001 | 0 | 0.262649 | 1.134228 | 0.545806 |
| 3 | 0.156620 | -0.766278 | 1 | 0.024530 | 0.587182 | -0.120015 |
| 4 | 0.479098 | 0.667054 | 1 | 0.229535 | 0.444961 | 0.319584 |
| ... | ... | ... | ... | ... | ... | ... |
| 95 | 0.049305 | 1.025803 | 0 | 0.002431 | 1.052271 | 0.050578 |
| 96 | -1.148978 | 0.323755 | 0 | 1.320151 | 0.104818 | -0.371988 |
| 97 | -0.626174 | 0.605153 | 0 | 0.392094 | 0.366211 | -0.378931 |
| 98 | 0.961855 | 0.403435 | 0 | 0.925165 | 0.162760 | 0.388046 |
| 99 | -0.379807 | -0.473364 | 1 | 0.144253 | 0.224074 | 0.179787 |
100 rows × 6 columns
X=df.drop(['Y'],axis=1)
y=df.Y
X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.25)
fig = px.scatter_3d(df,x='X1',y='X2',z='X5',color='Y')
fig.show()
fig = px.scatter_3d(df,x='X3',y='X4',z='X5',color='Y')
fig.show()
poly=SVC(kernel='poly')
poly.fit(X_train,y_train)
SVC(kernel='poly')
pred=poly.predict(X_test)
accuracy_score(y_test,pred)
0.76
rbf=SVC(kernel='rbf')
rbf.fit(X_train,y_train)
SVC()
pred=rbf.predict(X_test)
accuracy_score(y_test,pred)
0.84
sig=SVC(kernel='sigmoid')
sig.fit(X_train,y_train)
SVC(kernel='sigmoid')
pred=sig.predict(X_test)
accuracy_score(y_test,pred)
0.68